<?php
/*
 * SMTP 邮件发送类
 * Akon @ 2007/02/01
 */

class SMTPMail {
    var $_From;
    var $_mailFrom;
    var $_mailTo;
    var $_mailSubject;
    var $_mailBody;
    var $_host;
    var $_user;
    var $_pass;
    var $_port;
    var $_socket;
    var $_request;
    var $_conn;
    var $_debug;
    var $_log;
    
    /**
     * 初始化邮件
     * @param array $config
     */
    function SMTPMail($config = array()) {
        $this->_host = isset($config['host']) ? $config['host'] : '121.46.0.98';
        $this->_user = isset($config['user']) ? base64_encode($config['user']) : 'dflpvmkt@bluedoorinteractive.cn';
        $this->_pass = isset($config['pass']) ? base64_encode($config['pass']) : 'bluedoor2007';
        $this->_port = isset($config['port']) ? $config['port'] : 25;
        $this->_debug = isset($config['debug']) ? $config['debug'] : false;
        $this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($this->_socket) $this->Output(1001);
        else $this->Output(2001);
        $this->_conn = socket_connect($this->_socket, $this->_host, $this->_port);
        if ($this->_conn) $this->Output(1002);
        else $this->Output(2002);
        $this->Output(1003);
    }
    /**
     * 发送邮件
     * @param string $mailFrom 邮件发件地址
     * @param string $mailTo 邮件收件地址
     * @param string $mailSubject 邮件标题
     * @param string $mailBody 邮件内容
     * @param string $From 发件人名称
     */
    function SendMail($mailFrom, $mailTo, $mailSubject, $mailBody, $From) {
        if (!$this->CheckMail($mailFrom)) $this->Output(2003);
        elseif (empty($mailTo)) $this->Output(2004);
        elseif (empty($mailSubject)) $this->Output(2005);
        elseif (empty($mailBody)) $this->Output(2006);
        else {
            $this->_From = $From;
            $this->_mailFrom = $mailFrom;
            $this->_mailTo = $mailTo;
            $this->_mailSubject = $mailSubject;
            $this->_mailBody = $mailBody;
            $SendContent = "From: \"$this->_From\" <$this->_mailFrom>\r\n";
            //$SendContent .= "X-Mailer: PHP/".phpversion()."\r\n";
            $SendContent .= "To:{$this->_mailTo}\r\n";
            $SendContent .= "Content-Type:text/html;charset=utf-8\r\n";
            $SendContent .= "Subject:{$this->_mailSubject}\r\n\r\n";
            $SendContent .= $this->_mailBody;
            $this->docommand("EHLO HELO\r\n");
            $this->docommand("AUTH LOGIN\r\n");
            $this->docommand($this->_user . "\r\n");
            $this->docommand($this->_pass . "\r\n");
            $this->docommand("MAIL FROM: <" . $this->_mailFrom. ">\r\n");
            $this->docommand("RCPT TO: <" . $this->_mailTo . ">\r\n");
            $this->docommand("DATA\r\n");
            //$this->docommand("Content-Type:<text/html>\r\n");
            $this->docommand($SendContent ."\r\n.\r\n");
            $this->docommand("QUIT\r\n");
            socket_close($this->_socket);
        }
    }

    function CheckMail($email) {
        return (ereg( "^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov|mil|org|edu|int)$", $email));
    }

    function docommand($request) {
        $this->_request = $request;
        socket_write ($this->_socket, $this->_request, strlen ($this->_request));
        $this->Output(1004);
        $this->Output(1003);
    }

    function Output($msgcode) {
        switch ($msgcode) {
            case 1001 :
                $msg = "<b>SOCKET Create:</b> " . socket_strerror(socket_last_error()) . "";
                break;
            case 1002 :
                $msg = "<b>SOCKET Connection:</b> " . socket_strerror(socket_last_error()) . "";
                break;
            case 1003 :
                $msg = "<b>BACK:</b> <font color=\"green\">" . socket_read ($this->_socket, 1024) . "</font>";
                break;
            case 1004 :
                $msg = "<b>CWD:</b> <font color=\"red\">" . htmlentities($this->_request) . "</font>";
                break;
            case 2001 :
                $msg = "SOKET initialization failure, please check your network connections and parameters.";
                break;
            case 2002 :
                $msg = "SOKET failure to establish links, please check your network connections and parameters.";
                break;
            case 2003 :
                $msg = "<b>Error :</b> Mail FROM is invalid mail format.";
                break;
            case 2004 :
                $msg = "<b>Error :</b> Mail TO is null.";
                break;
            case 2005 :
                $msg = "<b>Error :</b> Mail Subject is null.";
                break;
            case 2006 :
                $msg = "<b>Error :</b> Mail Body is null.";
                break;
            case 2007 :
                $msg = "<b>Error :</b> ";
                break;
            default :
                $msg = $msgcode;
                break;
        }
        if ($this->_debug) $this->_log .= "{$msg}";
        //echo $msg;
    }

}

/*
 $SMTPMail = new SMTPMail("59.39.71.254", "angel", 'bluedoor2007', 25 ,true);
 $SMTPMail->SendMail("724631740@qq.com", "angel@extracare.com.cn", "this is mail title", "this is mail body");
 echo nl2br($SMTPMail->_log);
 */